$(function(){
	//收件人姓名栏
	$('#receiveName').blur(checkName);
	//收件人详细地址
	$('#fullAddress').blur(checkAddress);
	//邮政编码
	$('#postalCode').blur(checkCode);
	//电话
	$('#phone').blur(checkPhone);
	//手机
	$('#mobile').blur(checkMobile);
	
	

});


function checkName(){
	if($('#receiveName').val()==""){
		$('#fuck_0').text("姓名不能为空!").css('color','red');
		return false;
	}else{
		$('#fuck_0').text("");
		$('#fuck_0').append("<img src='images/01.gif' style='width:70%'/>");
		return true;
	}
}
//同一个用户不能保存地址一样的信息
function checkAddress(){
	/*$.ajax({
		   url: "order/productAction!checkUnique",
		   type: "POST",
		   data: 'address.id='+$('#addressId').val(),
		   dataType:'json',
		   success: function(data){
			alert(data.unique);
			if(data.unique){
				$('#fuck_1').text("");
				$('#fuck_1').append("<img src='images/01.gif' style='width:70%'/>");
				return true;
			}else{
				$('#fuck_1').text("地址已经存在!").css('color','red');
				return false;
			}
		   },
		   error:function(xhr,d1,d2){
			   return false;
		   }
		});*/
		if($('#fullAddress').val()==""){
			$('#fuck_1').text("收件人地址不能为空!").css('color','red');
			return false;
		}else{
			$('#fuck_1').text("");
			$('#fuck_1').append("<img src='images/01.gif' style='width:70%'/>");
			return true;
		}
}

function checkCode(){
	var reg = /^[0-9]{6}$/;
	if($('#postalCode').val()==""){
		$('#fuck_2').text("邮政编码不能为空!").css('color','red');
		return false;
	}else if(!reg.test($('#postalCode').val())){
		$('#fuck_2').text("请输入6位数字!").css('color','red');
		return false;
	}
	else{
		$('#fuck_2').text("");
		$('#fuck_2').append("<img src='images/01.gif' style='width:70%'/>");
		return true;
	}
}
function checkPhone(){
	var reg = /^([0-9]{3}\-|[0-9]{4}\-)*[0-9]{8}$/;
	if($('#phone').val()==""){
		$('#fuck_3').text("电话不能为空!").css('color','red');
		return false;
	}else if(!reg.test($('#phone').val())){
		$('#fuck_3').text("请输入正确的固话号码!").css('color','red');
		return false;
	}else{
		$('#fuck_3').text("");
		$('#fuck_3').append("<img src='images/01.gif' style='width:70%'/>");
		return true;
	}
}
function checkMobile(){
	var reg = /^[0-9]{11}$/;
	if($('#mobile').val()==""){
		$('#fuck_4').text("手机不能为空!").css('color','red');
		return false;
	}else if(!reg.test($('#mobile').val())){
		$('#fuck_4').text("手机格式不正确!").css('color','red');
		return false;
	}else{
		$('#fuck_4').text("");
		$('#fuck_4').append("<img src='images/01.gif' style='width:70%'/>");
		return true;
	}
}

function checkForm(){
	return checkName()&&checkAddress()&&checkCode()&&(checkPhone()||checkMobile());
}
//下拉选项change事件
function changeAdd(val){
	$.ajax({
		url:'order/productAction!optionAdd',
		type:'post',
		data:{'id':val},
		dataType:'json',
		success:function(data){
			if(data.address==null){
				//用户选择的添加新地址
				$('#receiveName').val("").attr('readonly',false).removeClass('oddAdd');
				$('#fullAddress').val("").attr('readonly',false).removeClass('oddAdd');
				$('#postalCode').val("").attr('readonly',false).removeClass('oddAdd');
				$('#phone').val("").attr('readonly',false).removeClass('oddAdd');
				$('#mobile').val("").attr('readonly',false).removeClass('oddAdd');
			}else{
				$('#receiveName').val(data.address.receiveName).attr('readonly',true).addClass('oddAdd');
				$('#fullAddress').val(data.address.fullAddress).attr('readonly',true).addClass('oddAdd');
				$('#postalCode').val(data.address.postalCode).attr('readonly',true).addClass('oddAdd');
				$('#phone').val(data.address.phone).attr('readonly',true).addClass('oddAdd');
				$('#mobile').val(data.address.mobile).attr('readonly',true).addClass('oddAdd');
				$('#addressId').val(data.address.id);
			}
	},
	error:function(xhr,d1,d2){
		
	}
	
	});
}